package com.example.kaogong.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.Notice;
import com.example.kaogong.sys.pojo.dto.NoticeDto;
import com.example.kaogong.sys.pojo.vo.NoticeDetailVo;
import com.example.kaogong.sys.pojo.vo.NoticeVo;
import com.example.kaogong.sys.service.INoticeService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "资讯接口")
@Slf4j
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @ApiOperation(value = "分页获取资讯基本信息")
    @GetMapping(value = "/basic/{current}/{size}")
    public RespBean<Page<NoticeVo>> getAllBasicNotice(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<NoticeVo>> respBean = noticeService.getAllBasicNotice(current, size);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据资讯编号获取资讯详细信息")
    @GetMapping(value = "/detail/{noticeId}")
    public RespBean<NoticeDetailVo> getNoticeDetailById(@PathVariable Integer noticeId) {
        Assert.notNull(noticeId, new ParamsErrorException("资讯编号不能为空"));
        log.info("noticeId:{}", noticeId);
        RespBean<NoticeDetailVo> respBean = noticeService.getNoticeDetailById(noticeId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页获取当前登录教师所发布的所有资讯基本信息")
    @GetMapping(value = "/curr/basic/{current}/{size}")
    public RespBean<Page<NoticeVo>> getCurrentTeacherAllNotice(@PathVariable Long current, @PathVariable Long size, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户名不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<NoticeVo>> respBean = noticeService.getCurrentTeacherAllNotice(current, size, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录教师发布资讯")
    @PostMapping(value = "/publish")
    public RespBean publishNotice(@RequestBody NoticeDto noticeDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户名不能为空"));
        log.info("request:{}", new Gson().toJson(noticeDto));
        RespBean respBean = noticeService.publishNotice(noticeDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据资讯编号删除资讯")
    @DeleteMapping(value = "/delete/{noticeId}")
    public RespBean deleteNoticeById(@PathVariable Integer noticeId) {
        Assert.notNull(noticeId, new ParamsErrorException("资讯编号不能空"));
        log.info("noticeId:{}", noticeId);
        RespBean respBean = noticeService.deleteNoticeById(noticeId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据资讯编号修改资讯信息")
    @PutMapping(value = "/update/{noticeId}")
    public RespBean updateNoticeById(@RequestBody NoticeDto noticeDto, @PathVariable Integer noticeId) {
        Assert.notNull(noticeId, new ParamsErrorException("资讯编号不能为空"));
        log.info("noticeId:{}, request:{}", noticeId, new Gson().toJson(noticeDto));
        RespBean respBean = noticeService.updateNoticeById(noticeDto, noticeId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }


}
